package view.backing;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;

import javax.faces.context.FacesContext;

import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.view.rich.event.QueryEvent;
import oracle.adf.view.rich.render.ClientEvent;

import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;

import view.util.JSFUtils;


public class TestBean
{

  private QueryEvent qEvent = null;


  public TestBean()
  {
    super();
  }

  public void toggleBusyPopup(boolean isShown)
  {
    FacesContext context = FacesContext.getCurrentInstance();
    RichPopup popup = (RichPopup) JSFUtils.findComponent("busyPopup");
    ExtendedRenderKitService service =
      Service.getRenderKitService(context, ExtendedRenderKitService.class);

    if (isShown)
    {
      service.addScript(context,
                        "var popup = AdfPage.PAGE.findComponent(\"" +
                        popup.getClientId(context) + "\"); popup.show();");
    }
    else
    {
      service.addScript(context,
                        "var popup = AdfPage.PAGE.findComponent(\"" +
                        popup.getClientId(context) + "\"); popup.hide();");
    }
    return;
  }


  public void processQuery(QueryEvent queryEvent)
  {
    setQEvent(queryEvent); // save teh query event for the method that really fires the query to use.
    toggleBusyPopup(true); // Fires the popup, which when shown, fires a server listener that fires the query.

  }

  /**
   *The method that actually fires the search query on the server.
   * This is invoked by a client JS method on displaying the popup for wait message.
   * @param clientEvent
   */
  public void processSearchQuery(ClientEvent clientEvent)
  {
    // Do query : //
    invokeMethodExpression("#{bindings.EmployeeSearchQuery.processQuery}",
                           Object.class, QueryEvent.class, getQEvent());
    AdfFacesContext.getCurrentInstance().addPartialTarget(JSFUtils.findComponent("resId1"));
    toggleBusyPopup(false);
  }


  private Object invokeMethodExpression(String expr, Class returnType,
                                        Class[] argTypes, Object[] args)
  {
    FacesContext fc = FacesContext.getCurrentInstance();
    ELContext elctx = fc.getELContext();
    ExpressionFactory elFactory =
      fc.getApplication().getExpressionFactory();
    MethodExpression methodExpr =
      elFactory.createMethodExpression(elctx, expr, returnType, argTypes);
    return methodExpr.invoke(elctx, args);
  }

  private Object invokeMethodExpression(String expr, Class returnType,
                                        Class argType, Object argument)
  {
    return invokeMethodExpression(expr, returnType, new Class[]
        { argType }, new Object[]
        { argument });
  }

  public void setQEvent(QueryEvent pQEvent)
  {
    this.qEvent = pQEvent;
  }

  public QueryEvent getQEvent()
  {
    return qEvent;
  }
}

